/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   PrefabConfig.cs
 * CreateData   :   2023/7/9 17:14:47
 * UnityVersion :   2021.3.20f1c1
 * Description  :   描述
************************************************/

#if UNITY_EDITOR
using UnityEditor;
#endif

using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random = UnityEngine.Random;
using System.Linq;
using Object = UnityEngine.Object;
using System.IO;

namespace Main
{
    public class PrefabConfig : ScriptableObjectConfigBase<PrefabInfo>
    {
#if UNITY_EDITOR
        /// <summary>
        /// 生成 ID
        /// </summary>
        private string SpawnID(string name = "ZZ_Prefab")
        {
            string _name = name;
            int index = 0;
            while (datas.ContainsKey(name))
            {
                index++;
                name = $"{_name}_{index}";
            }
            return name;
        }
        [OnInspectorGUI]
        private void OnInspectorGUI()
        {
            Event currentEvent = Event.current;
            Rect dropArea = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            switch (currentEvent.type)
            {
                case EventType.DragUpdated:
                case EventType.DragPerform:
                    if (!dropArea.Contains(currentEvent.mousePosition))
                        return;

                    if (DragAndDrop.objectReferences.Any(obj => IsPrefabPath(obj)))
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                        DragAndDrop.AcceptDrag();
                        if (currentEvent.type == EventType.DragPerform)
                        {
                            foreach (Object prefab in DragAndDrop.objectReferences)
                            {
                                if (IsPrefabPath(prefab))
                                {
                                    if (!datas.Any(item => item.Value.prefab == prefab))
                                    {
                                        PrefabInfo info = new PrefabInfo();
                                        info.prefab = (GameObject)prefab;
                                        datas.Add(SpawnID(prefab.name), info);
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        }
        private bool IsPrefabPath(Object obj)
        {
            return (obj != null && PrefabUtility.GetPrefabAssetType(obj) != PrefabAssetType.NotAPrefab);
        }
        [Button("添加"), HorizontalGroup("Button")]
        private void Add()
        {
            datas.Add(SpawnID(), new PrefabInfo());
        }
        [Button("清空Null"), HorizontalGroup("Button")]
        private void ClearNull()
        {
            List<string> recoveryID = new List<string>();
            foreach (var item in datas)
            {
                if (item.Value.prefab == null)
                {
                    recoveryID.Add(item.Key);
                }
            }
            foreach (var item in recoveryID)
            {
                datas.Remove(item);
            }
        }
        [Button("清空"), HorizontalGroup("Button")]
        private void Clear()
        {
            datas.Clear();
        }
#endif
    }
    public struct PrefabInfo
    {
#if UNITY_EDITOR
        [HorizontalGroup(Width = 40), HideLabel,
        PreviewField(ObjectFieldAlignment.Center, Height = 40), AssetsOnly]
#endif
        public GameObject prefab;
        [HorizontalGroup, HideLabel]
        public string description;
    }
}